package Tool;

import Javabean.Staff;
import Javabean.User;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;

public class Function_Staff_Delete {
    public Function_Staff_Delete(Connection conn, ArrayList<Staff> staffs) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        new InitJdialog(jDialog);
        jDialog.setSize(350,150);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        jDialog.setTitle("删除职工信息");

        //设置组件
        JTextField idtxt = new JTextField();

        JLabel id = new JLabel("ID");

        JButton truejbt = new JButton("确定");


        id.setBounds(20,12,70,20);

        idtxt.setBounds(80,15,200,20);

        truejbt.setBounds(130,60,70,30);


        //添加组件
        jDialog.getContentPane().add(id);

        jDialog.getContentPane().add(idtxt);

        jDialog.getContentPane().add(truejbt);

        //添加监听
        truejbt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //获取输入数据

                    int id = Integer.parseInt(idtxt.getText());

                    //判断数据是否正确
                    boolean flag = false;
                    flag =  MySQL_IsStaffId_Exist.isIdExist(conn,id);
                    if(!flag){

                        //如果输入不正确，执行catch
                        int a = 1 / 0;

                    }

                    //在数据库中删除
                    MySQL_StaffDelete.staffDelete(conn,id);

                    //在集合中删除
                    Arraylist_Staff_Delete.staffDelete(staffs,id);


                } catch (Exception ex) {

                    //输入数据错误处理
                    JDialog errorjDialog = new JDialog();
                    new Init_ShuRuError_Jdialog(errorjDialog);

                    throw new RuntimeException(ex);
                }

                //创建成功的提醒弹框
                JDialog Throw = new JDialog();
                new InitJdialog(Throw);
                Throw.setBounds(600,100,200,100);
                JLabel truethrow = new JLabel("删除成功");
                truethrow.setBounds(65,20,80,30);
                Throw.getContentPane().add(truethrow);

                Throw.setVisible(true);

                //关闭弹窗
                jDialog.dispose();
            }
        });

        //让弹框显示出来
        jDialog.setVisible(true);
    }
}
